package com.tys.algorithm.beginner;

public class Code4_Frandom {
    public static void main(String[] args) {
        int testTimes = 1000000;

//        random1_5(testTimes);
//        random0_7(testTimes, 8);
        random0_6(testTimes, 8);
        random1_7(testTimes, 8);
        random01_01(testTimes, 2);

    }

    public static void random1_5(int testTimes) {
        System.out.println("======================");
        int count = 0;
        for (int i = 0; i < testTimes; i++) {
            if (f2() == 0) {
                count++;
            }
        }
        //求概率
        System.out.println((double) count / (double) testTimes);
    }

    public static void random0_7(int testTimes, int x) {
        System.out.println("======================");
        int[] counts = new int[x];
        for (int i = 0; i < testTimes; i++) {
            int num = f3();
            counts[num]++;
        }

        //求概率
        for (int i = 0; i < x; i++) {
            System.out.println(i + "这个数出现了" + counts[i] + "次");
        }
    }

    public static void random0_6(int testTimes, int x) {
        System.out.println("======================");
        int[] counts = new int[x];
        for (int i = 0; i < testTimes; i++) {
            int num = f4();
            counts[num]++;
        }

        //求概率
        for (int i = 0; i < x; i++) {
            System.out.println(i + "这个数出现了" + counts[i] + "次");
        }
    }

    public static void random1_7(int testTimes, int x) {
        System.out.println("======================");
        int[] counts = new int[x];
        for (int i = 0; i < testTimes; i++) {
//            int num = g();
            int num = g2();
            counts[num]++;
        }

        //求概率
        for (int i = 0; i < x; i++) {
            System.out.println(i + "这个数出现了" + counts[i] + "次");
        }
    }

    public static void random01_01(int testTimes, int x) {
        System.out.println("======================");
        int[] counts = new int[x];
        for (int i = 0; i < testTimes; i++) {
//            int num = x();
            int num = y();
            counts[num]++;
        }

        //求概率
        for (int i = 0; i < x; i++) {
            System.out.println(i + "这个数出现了" + counts[i] + "次");
        }
    }

    //[1,5] 等概率
    public static int f1() {
        return (int) (Math.random() * 5) + 1;
    }

    //0,1发生器，等概率返回
    public static int f2() {
        int ans = 0;
        do {
            ans = f1();
        } while (ans == 3);
        //1 2 返回0, 4 5 返回1
        return ans < 3 ? 0 : 1;
    }

    //得到二进制位：000~111的等概率,0-7等概率返回
    public static int f3() {
        return ((f2() << 2) + (f2() << 1) + (f2() << 0));
    }

    //0~6等概率返回
    public static int f4() {
        int ans = 0;
        do {
            ans = f3();
        } while (ans == 7);
        return ans;
    }

    //1~7等概率
    public static int g() {
        return f4() + 1;
    }

    public static int g2() {
        int ans = 0;
        do {
            ans = f3();
        } while (ans == 0);
        return ans;
    }

    //0 1 不等概率
    public static int x() {
        return Math.random() < 0.84 ? 0 : 1;
    }

    //0 1 等概率
    public static int y() {
        int ans = 0;
        do {
            ans = x();
        } while (ans == x());
        return ans;
    }
}
